package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Member;
import org.springframework.context.ApplicationContext;
import redis.clients.jedis.JedisPool;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//权限过滤器： 判断用户是否登录，如果登录则放行。如果没有登录，某些资源不让放行
public class AuthFilter implements Filter {

    JedisPool jedisPool;

    private ServletContext servletContext;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.servletContext = filterConfig.getServletContext();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //congSpringmvc容器中获取JedisPool对象
        if(this.jedisPool == null){
            ApplicationContext wac =
                    (ApplicationContext) servletContext.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springmvc");
            this.jedisPool = wac.getBean(JedisPool.class);
        }


        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String requestURI = request.getRequestURI();

        //放行一些页面  比如：登录页 js css 图片
        if(requestURI.equals("/pages/login.html")
                ||requestURI.equals("/pages/index.html")
                ||requestURI.equals("/member/login.do")
                ||requestURI.equals("/order/submit.do")
                ||requestURI.contains("/validateCode/")
                ||requestURI.contains("/setmeal/")
                ||requestURI.equals("/pages/m_setmeal.html")
                ||requestURI.equals("/pages/setmeal.html")
                ||requestURI.contains("/pages/setmeal_detail")
                ||requestURI.equals("/pages/orderInfo.html")
                ||requestURI.equals("/")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        // 放行静态资源
        if(requestURI.contains("/fonts/")
                ||requestURI.contains("/css/")
                ||requestURI.contains("/img/")
                ||requestURI.contains("/plugins")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        //剩下的需要判断用户是否登录，只有登录了才能放行
        //1、先从cookie中获取用户的手机号

        Cookie[] cookies = request.getCookies();
        if(cookies!=null && cookies.length>0){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if("login_member_telephone".equals(name)){
                    String telephone = cookie.getValue();
                    //2、根据手机号从redis中获取用户信息
                    String memberStr = jedisPool.getResource().get(telephone);
                    if(memberStr!=null && memberStr.length()>0){
                        Member member = JSON.parseObject(memberStr, Member.class);
                        if(member!=null && member.getPhoneNumber().equals(telephone)){
                            //登陆过,放行
                            filterChain.doFilter(servletRequest,servletResponse);
                            return;
                        }
                    }
                }
            }
        }

        //如果没有登录跳转都登录页  重定向到登录
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        httpServletResponse.sendRedirect("/pages/login.html");
    }

    @Override
    public void destroy() {

    }
}
